package com.romotc;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;


public class DailIpTools extends IpTools {


	public DailIpTools()
	{
		
	}
	
	public static boolean changeIp(int maxTry) {
		return true;
	}

	
	private static void reconnect() {
		while(true)
		try {
			disconnect();
			Thread.sleep(2000);
			
			//放置连接后没有网络访问权限
			String ip = getIp();
			if(StringUtils.isNotBlank(ip)) {
				break;
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		
	}


//	static WebClient webClient = new WebClient();
	public static String getIp() {

		try {
//			HtmlPage page = webClient.getPage("http://www.ip138.com/ip2city.asp");
//			HtmlElement ipinfo = page.getElementsByTagName("center").get(0);
			String ipStr = null/*ipinfo.asText()*/;
//			int start = ipStr.indexOf("[") + 1;
//			int end = ipStr.indexOf("]");
//			ipStr = ipStr.substring(start,end);
//			System.out.println(ipStr);
			return ipStr;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
		
	}
	public static InetAddress[] getLocalIPs() {
		InetAddress[] mArLocalIP = null;
		if (mArLocalIP == null) {
			try {
				mArLocalIP = InetAddress.getAllByName(InetAddress
						.getLocalHost().getHostName());
			} catch (Exception e) {
				System.out.println("getHostIP   error ");
			}
		}
		return mArLocalIP;
	}

	/**
	 * 连接宽带
	 * 
	 * @param cname :
	 *            连接名，如:宽带连接
	 * @param uname :
	 *            宽带连接的用户名
	 * @param pwd
	 *            :宽带连接的密码
	 */
	static void connect(String dailInfo) {
		try {
			System.out.println("连接拨号:" + dailInfo);
			// 单独的进程中执行指定的字符串命令
			Process p = Runtime.getRuntime().exec(
					"rasdial.exe " + dailInfo);

			// 等待当前线程执行终止
			p.waitFor();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 断开宽带连接
	 */
	static void disconnect() {
		try {
			System.out.println("断开连接");
			Process p = Runtime.getRuntime().exec("rasdial.exe /DISCONNECT");
			p.waitFor();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args){}
	
	private static DailIpTools instance;
	public static DailIpTools getInstance() {
		if(instance == null) {
			instance = new DailIpTools();
		}
		
		return instance;
	}

	/**
	 * 切换拨号账户  网通 || 移动
	 */
	public static void switchDailAccount() {

	}
	

}
